package com.itheima.controller;


import com.itheima.pojo.Category;
import com.itheima.pojo.Result;
import com.itheima.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //新增文章分类
    @PostMapping
    public Result add(@RequestBody @Validated Category category)
    {

        categoryService.add(category);

        return  Result.success();

    }


    //文章分类列表

    @GetMapping
    public Result<List<Category>> Get()
    {

        List<Category> list=categoryService.get();


        return Result.success(list);

    }

    //获取文章分类详情
    @GetMapping("/detail")
    public Result<Category> findById(Integer id)
    {
        Category category=categoryService.findById(id);
        return Result.success(category);
    }

    //更新文章分类
    @PutMapping
    public Result update(@RequestBody @Validated Category category)
    {

        categoryService.update(category);

        return  Result.success();


    }


    //删除文章分类
    @DeleteMapping
    public Result delete(Integer id)
    {
        categoryService.delete(id);
        return Result.success();
    }









}
